package com.xybian.services.impl;

import com.xybian.entity.SysUser;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户详细信息 impl
 *
 * @author SongRenShuo
 * @date 2023-12-23
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserDetailsImpl implements UserDetails {

    /**
     * 登录用户信息
     */
    private SysUser sysUser;

    /**
     * 用户权限
     */
    private List<String> permissions;

    /**
     * 获取权限
     *
     * @return 集合<？ 扩展授予权限>
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        // 提供用户的权限列表信息
        return permissions.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }

    /**
     * 获取密码
     *
     * @return 字符串
     */
    @Override
    public String getPassword() {
        return sysUser.getPassword();
    }

    /**
     * 获取用户名
     *
     * @return 字符串
     */
    @Override
    public String getUsername() {
        return sysUser.getUserName();
    }

    /**
     * 帐户是否未过期
     *
     * @return boolean
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 帐户是否未锁定
     *
     * @return boolean
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 凭据是否未过期
     *
     * @return boolean
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否启用
     *
     * @return boolean
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
